//
//  WPCancelTransAlertView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/12.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPCancelTransAlertView.h"
#import "WPTransCancelModel.h"

@interface WPCancelTransAlertView ()
@property (nonatomic, strong) UIScrollView * backScrollView;/**< 背景滚动视图 */
@property (nonatomic, strong) UILabel * detailTitleLabel;/**< 具体标题 */
@property (nonatomic, strong) UILabel * positionLabel;/**< 位置 */
@property (nonatomic, strong) UIView * grayView;/**< 灰色背景 */
@property (nonatomic, strong) UITextField * buyinPriceTextField;/**< 买入价 */
@property (nonatomic, strong) UITextField * sellPriceTextField;/**< 卖出价 */
@property (nonatomic, strong) UIButton * cancelBtn;/**< 撤回按钮 */
@property (nonatomic, strong) NSArray * dataSource;/**< 数据源 */
@property (nonatomic, copy) void (^complete)(NSString *result);/**< 结果block */
- (void)initializeAppearance;/**< 一般界面初始化 */
@end
@implementation WPCancelTransAlertView
+ (WPCancelTransAlertView *)showViewWithSize:(CGRect)rect andWithTitle:(NSString *)title andWithDataSource:(NSArray *)dataSource WithComplete:(void (^)(NSString *))complete {
    WPCancelTransAlertView * view = [[WPCancelTransAlertView alloc]initWithTitle:title withSize:rect andDataSource:dataSource WithComplete:complete];
    [view show];
    return view;
}

- (instancetype)initWithTitle:(NSString *)title withSize:(CGRect)rect andDataSource:(NSArray *)dataSource WithComplete:(void (^)(NSString *))complete {
    self = [super initWithFrame:rect withTitle:title];
    if (self) {
        _dataSource = dataSource;
        _complete = complete;
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeAppearance {
    [self addSubview:self.backScrollView];
    [_backScrollView addSubview:self.grayView];
    [_backScrollView addSubview:self.detailTitleLabel];
    [_backScrollView addSubview:self.positionLabel];
    [_backScrollView addSubview:self.buyinPriceTextField];
    [_backScrollView addSubview:self.sellPriceTextField];
    [_backScrollView addSubview:self.cancelBtn];
}
#pragma mark - responds events
- (void)respondsToCancelBtn {
    [self hide];
    _complete(@"撤销");
}
#pragma mark - getter
- (UIScrollView *)backScrollView {
    if (!_backScrollView) {
        _backScrollView = ({
            UIScrollView * view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40*DHFlexibleHorizontalMutiplier()+2, 270*DHFlexibleHorizontalMutiplier(), 180*DHFlexibleHorizontalMutiplier())];
            view.contentSize = CGSizeMake(0, CGRectGetMaxY(self.cancelBtn.frame)+40*DHFlexibleHorizontalMutiplier());
            view;
        });
    }
    return _backScrollView;
}
- (UILabel *)detailTitleLabel {
    if (!_detailTitleLabel) {
        _detailTitleLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(12*DHFlexibleHorizontalMutiplier(), 0, 250*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(61, 61, 61, 1);
            lab.numberOfLines = 0;
            lab.text = [(WPTransCancelModel *)_dataSource[0] goodsName];
            lab;
        });
    }
    return _detailTitleLabel;
}
- (UILabel *)positionLabel {
    if (!_positionLabel) {
        _positionLabel = ({
//            NSMutableString * str = [NSMutableString stringWithString:_dataSource[1][0]];
//            for (int i = 1; i < [_dataSource[1] count]; i++) {
//                if (i % 3 == 0) {
//                    [str appendString:@"\n"];
//                    [str appendString:_dataSource[1][i]];
//                    continue;
//                }
//                [str appendString:[NSString stringWithFormat:@"  %@  ", _dataSource[1][i]]];
//
//            }
//            CGRect rect = [WPCalculateRectHandle calculateSizeWithFont:14*DHFlexibleHorizontalMutiplier() Text:str Width:250*DHFlexibleHorizontalMutiplier()];
//            NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString: str];
//            NSMutableParagraphStyle *warnParagraph = [[NSMutableParagraphStyle alloc] init];
//            warnParagraph.lineSpacing = 5*DHFlexibleHorizontalMutiplier();//行间距
//            [attributedStr addAttribute:NSParagraphStyleAttributeName value:warnParagraph range:NSMakeRange(0, str.length)];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.detailTitleLabel.frame), CGRectGetMaxY(self.detailTitleLabel.frame), CGRectGetWidth(self.detailTitleLabel.bounds), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.numberOfLines = 0;
            lab.text = [(WPTransCancelModel *)_dataSource[0] ticketName];
            [lab drawTextInRect:UIEdgeInsetsInsetRect(lab.frame, UIEdgeInsetsMake(0.1, 0.1, 0.1, 0))];
            lab;
        });
    }
    return _positionLabel;
}
- (UIView *)grayView {
    if (!_grayView) {
        _grayView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetMaxY(self.positionLabel.frame)+10*DHFlexibleHorizontalMutiplier() )];
            view.backgroundColor = BGCOLOR;
            view;
        });
    }
    return _grayView;
}
- (UITextField *)buyinPriceTextField {
    if (!_buyinPriceTextField) {
        _buyinPriceTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.detailTitleLabel.frame), CGRectGetMaxY(self.grayView.frame)+10*DHFlexibleHorizontalMutiplier(), 145*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(61, 61, 61, 1);
            lab.text = @"买入价：";
            text.leftView = lab;
            text.userInteractionEnabled = NO;
            text.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(61, 61, 61, 1);
            text.text = [NSString stringWithFormat:@"¥ %@", [(WPTransCancelModel *)_dataSource[0] gerBuyMoent]];
            text;
        });
    }
    return _buyinPriceTextField;
}
- (UITextField *)sellPriceTextField {
    if (!_sellPriceTextField) {
        _sellPriceTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.buyinPriceTextField.frame), CGRectGetMinY(self.buyinPriceTextField.frame), 125*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(61, 61, 61, 1);
            lab.text = @"转卖价：";
            text.leftView = lab;
            text.userInteractionEnabled = NO;
            text.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(61, 61, 61, 1);
            text.text = [NSString stringWithFormat:@"¥ %@", [(WPTransCancelModel *)_dataSource[0] salePrice]];
            text;
        });
    }
    return _sellPriceTextField;
}
- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(35*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.buyinPriceTextField.frame)+20*DHFlexibleHorizontalMutiplier(), 200*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"确认撤回" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            [btn addTarget:self action:@selector(respondsToCancelBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;

        });
    }
    return _cancelBtn;
}
@end
